/*
 *	Yalala
 *	
 *	Copyright © MediaMonks B.V.
 *	All rights reserved.
 *	
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions are met:
 *	1. Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	2. Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *	3. All advertising materials mentioning features or use of this software
 *	   must display the following acknowledgement:
 *	   This product includes software developed by MediaMonks B.V.
 *	4. Neither the name of MediaMonks B.V. nor the
 *	   names of its contributors may be used to endorse or promote products
 *	   derived from this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *	DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
 *	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */

package com.mediamonks.yalala.ui 
{
	import nl.acidcats.yalog.common.Levels;

	import temple.codecomponents.buttons.CodeLabelButton;
	import temple.codecomponents.form.components.CodeCheckBox;
	import temple.common.enum.Direction;
	import temple.common.enum.Orientation;
	import temple.common.interfaces.IFocusable;
	import temple.ui.focus.TabFocusManager;
	import temple.ui.form.Form;
	import temple.ui.form.FormEvent;
	import temple.ui.form.components.CheckBox;
	import temple.ui.form.services.ObjectFormService;
	import temple.ui.layout.LayoutContainer;
	import temple.ui.tooltip.ToolTip;
	import temple.utils.types.StringUtils;

	import com.mediamonks.yalala.data.vo.SettingsData;

	import flash.events.MouseEvent;

	/**
	 * @author Thijs Broerse
	 */
	public class Footer extends LayoutContainer implements IFocusable
	{
		private var _levelsForm:Form;
		private var _settingsForm:Form;
		private var _focusManager:TabFocusManager;

		public function Footer(settings:SettingsData)
		{
			super(NaN, NaN, Orientation.HORIZONTAL, Direction.ASCENDING, 10);
			
			this._focusManager = new TabFocusManager(false);
			
			this.addButton("Clear", this.handleClearButtonClick, "Clear all messages");
			this.addButton("Copy", this.handleCopyButtonClick, "Copy (filtered) messages to your clipboard");
			this.addButton("Export", this.handleExportButtonClick, "Export to CSV file");
			
			this._levelsForm = new Form(new ObjectFormService(settings.levels));
			this._levelsForm.addEventListener(FormEvent.SUBMIT_SUCCESS, this.handleFormEvent);
			this._levelsForm.tabFocusManager.loop = false;
			this._focusManager.add(this._levelsForm.tabFocusManager);
			
			// check boxes
			this.addCheckBox(Levels.DEBUG, settings.levels, "toggle show/hide debug messages");
			this.addCheckBox(Levels.INFO, settings.levels, "toggle show/hide info messages");
			this.addCheckBox(Levels.WARN, settings.levels, "toggle show/hide warnings");
			this.addCheckBox(Levels.FATAL, settings.levels, "toggle show/hide fatal warnings");
			this.addCheckBox(Levels.ERROR, settings.levels, "toggle show/hide error messages");
			this.addCheckBox(Levels.STATUS, settings.levels, "toggle show/hide status messages");
			
			this._settingsForm = new Form(new ObjectFormService(settings));
			this._settingsForm.addEventListener(FormEvent.SUBMIT_SUCCESS, this.handleFormEvent);
			
			var cbx:CheckBox = new CodeCheckBox("Auto scroll", true, settings.autoScroll);
			this.addChild(cbx);
			this._settingsForm.addElement(cbx, SettingsData.AUTO_SCROLL);
			cbx.submitOnChange = true;
			ToolTip.add(cbx, "If this checkbox is selected the list will automaticaly scroll down if a new message is received.");
			
			this.childrenLiquidProperties = {verticalCenter: 0};
		}

		private function addButton(label:String, clickHandler:Function, tooltipText:String):void 
		{
			var btn:CodeLabelButton = new CodeLabelButton(label);
			this.addChild(btn);
			btn.addEventListener(MouseEvent.CLICK, clickHandler);
			this._focusManager.add(btn);
			
			ToolTip.add(btn, tooltipText);
		}
		
		private function addCheckBox(level:uint, levels:Vector.<Boolean>, tooltipText:String):void 
		{
			var cbx:CodeCheckBox = new CodeCheckBox(StringUtils.ucFirst(Levels.NAMES[level]), true, levels[level]);
			this.addChild(cbx);
			this._levelsForm.addElement(cbx, String(level));
			cbx.submitOnChange = true;
			cbx.textField.textColor = LevelColors.COLORS[level];
			
			ToolTip.add(cbx, tooltipText);
		}
		
		private function handleClearButtonClick(event:MouseEvent):void 
		{
			this.dispatchEvent(new UIEvent(UIEvent.CLEAR));
		}

		private function handleCopyButtonClick(event:MouseEvent):void 
		{
			this.dispatchEvent(new UIEvent(UIEvent.COPY));
		}
		
		private function handleExportButtonClick(event:MouseEvent):void 
		{
			this.dispatchEvent(new UIEvent(UIEvent.EXPORT));
		}
		
		private function handleFormEvent(event:FormEvent):void 
		{
			switch (event.type)
			{
				case FormEvent.SUBMIT_SUCCESS:
					this.dispatchEvent(new UIEvent(UIEvent.UPDATE));
					break;
			}
		}
		
		public function get focus():Boolean
		{
			return this._focusManager.focus;
		}
		
		public function set focus(value:Boolean):void
		{
			this._focusManager.focus = value;
		}
		
		public function get focusManager():TabFocusManager
		{
			return this._focusManager;
		}
	}
}
